package com.kantboot.business.common.web.i18n.admin.controller;

import com.kantboot.business.common.dao.i18n.repository.BusCommonCarouselImageI18nRepository;
import com.kantboot.business.common.domain.i18n.BusCommonCarouselImageI18n;
import com.kantboot.global.rest.result.CommonSuccessStateCodeAndMsg;
import com.kantboot.util.common.result.RestResult;
import com.kantboot.util.core.controller.BaseAdminController;
import com.kantboot.util.core.redis.RedisUtil;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/business-common-web/admin/carouselImageI18n")
public class BusCommonCarouselImageI18nControllerOfAdmin extends BaseAdminController<BusCommonCarouselImageI18n,Long> {

    @Resource
    private BusCommonCarouselImageI18nRepository repository;

    @Resource
    private RedisUtil redisUtil;

    /**
     * 修改轮播图国际化信息
     */
    @RequestMapping(value = "/newSave")
    public RestResult<BusCommonCarouselImageI18n> newSave(@RequestBody BusCommonCarouselImageI18n busCommonCarouselImageI18n) {
        busCommonCarouselImageI18n.setId(null);
        // 根据轮播图id和语言编码查询轮播图国际化信息
        BusCommonCarouselImageI18n carouselImageI18n = repository.getByLanguageCodeAndCarouselImageId(busCommonCarouselImageI18n.getLanguageCode(), busCommonCarouselImageI18n.getCarouselImageId());
        if (carouselImageI18n != null) {
            busCommonCarouselImageI18n.setId(carouselImageI18n.getId());
        }
        BusCommonCarouselImageI18n result = repository.save(busCommonCarouselImageI18n);
        // 删除缓存
        redisUtil.delete("carouselImageI18n:" + busCommonCarouselImageI18n.getLanguageCode());
        return RestResult.success(result, CommonSuccessStateCodeAndMsg.SAVE_SUCCESS);
    }

    @RequestMapping(value = "/clearFileIdOfImageByLanguageCodeAndCarouselImageId")
    public RestResult<?> clearFileIdOfImageByLanguageCodeAndCarouselImageId(
            @RequestParam(value = "languageCode") String languageCode,
            @RequestParam(value = "carouselImageId") Long carouselImageId) {
        repository.clearFileIdOfImageByLanguageCodeAndCarouselImageId(languageCode, carouselImageId);
        // 删除缓存
        redisUtil.delete("carouselImageI18n:" + languageCode);
        return RestResult.success(null,CommonSuccessStateCodeAndMsg.SAVE_SUCCESS);
    }

}
